---title: "Analysis"---::: {.cell}<img src="gender_inequality_pie_chart_by_level.gif" alt="Kadın-Erkek Farkı Isı Dağılım Grafiği" style="width: 100%; display: block; margin: 0 auto;" />::: {.cell}<img src="gender_difference_heatmap.gif" alt="Kadın-Erkek Farkı Isı Dağılım Grafiği" style="width: 100%; display: block; margin: 0 auto;" />```{python}import pandas as pd# Veri setini yükle ve tamamen boş sütunları kaldırdf = pd.read_excel('/home/mami/emu430/emu430-fall2024-team-education_atlas/data/yıllara_göre_egitim.xlsx')df = df.dropna(axis=1, how='all')# Yılları ve indeks aralıklarını tanımlayears =list(range(2008, 2024)) # 2008'den 2023'e kadarindex_ranges = [(1+ i *82, 1+ (i +1) *82) for i inrange(len(years))]# Her yıl için alt veri çerçevelerini oluşturyearly_data = {}for year, (start, end) inzip(years, index_ranges): yearly_data[year] = df.iloc[start:end]# İlk birkaç yılın veri çerçevelerini kontrol etsample_years = {year: data.head(1) for year, data inlist(yearly_data.items())[:3]}# İlk iki satırı başlık olarak kullan ve eksik değerleri doldurheader1 = df.iloc[0].bfill()header2 = df.iloc[1].bfill()# Çok seviyeli (MultiIndex) başlıklar oluşturdf.columns = pd.MultiIndex.from_tuples(zip(header1, header2))# İlk iki satırı kaldır ve veri setini sıfırladf = df.iloc[2:].reset_index(drop=True)# İlk 3 sütun için tek seviyeli başlıklar oluşturfirst_three_headers = ['Yıl', 'İl Kodu', 'İl Adı']# Ana başlıklar ve alt başlıkları tanımlamain_headers = ['Genel toplam', 'Okuma yazma bilmeyen', 'Okuma yazma bilen fakat bir okul bitirmeyen','İlkokul', 'İlköğretim', 'Ortaokul ve dengi meslek', 'Lise ve dengi meslek','Yüksekokul veya fakülte', 'Yüksek lisans', 'Doktora', 'Bilinmeyen']sub_headers = ['Toplam', 'Erkek', 'Kadın']# Çok seviyeli başlıkları oluşturremaining_headers = [(main, sub) for main in main_headers for sub in sub_headers]# Tüm başlıkları birleştirfinal_headers = first_three_headers + remaining_headers[:df.shape[1] -len(first_three_headers)]df.columns = pd.MultiIndex.from_tuples( [(header, '') ifisinstance(header, str) else header for header in final_headers])ifisinstance(df.columns, pd.MultiIndex): df.columns = ['_'.join(map(str, col)).strip() ifisinstance(col, tuple) else col for col in df.columns]# Yıl sütununu bulyear_column = [col for col in df.columns if'Yıl'in col]ifnot year_column:raiseKeyError("Veri setinde 'Yıl' sütunu bulunamadı.")year_column = year_column[0]import matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport seaborn as snsfrom PIL import Imageimport osimport plotly.express as pximport plotly.graph_objects as goimport jsondef create_turkey_interactive_map(df):""" Türkiye haritası üzerinde şehir bazında kadın-erkek farkını gösteren etkileşimli bir harita oluşturur. Yıl seçildiğinde, ilgili yılın haritası gösterilir. """ city_column = [col for col in df.columns if'İl Adı'in col][0] year_column = [col for col in df.columns if'Yıl'in col][0] years = df[year_column].dropna().unique()# JSON dosyasını doğrula geojson_path ='data/tr-cities.json'ifnot os.path.exists(geojson_path):raiseFileNotFoundError("GeoJSON dosyası bulunamadı: data/tr-cities.json")withopen(geojson_path) as f: geojson = json.load(f) dropdown_options = [ {'label': str(year), 'method': 'update', 'args': [{'visible': [year == y for y in years]}, {'title': f'Kadın-Erkek Farkı Haritası - {year}'}]}for year in years ] fig = go.Figure()for year in years: df_year = df.loc[df[year_column] == year].copy() df_year = df_year.copy() df_year['Difference'] = df_year['Genel toplam_Kadın'] - df_year['Genel toplam_Erkek'] fig.add_trace( go.Choropleth( geojson=geojson, locations=df_year[city_column], z=df_year['Difference'], featureidkey='properties.number', colorscale='RdBu', colorbar_title='Fark', visible=(year == years[0]) ) ) fig.update_layout( title_text='Yıla Göre Kadın-Erkek Farkı Haritası', geo=dict( fitbounds="locations", visible=True ), updatemenus=[{'buttons': dropdown_options, 'direction': 'down', 'showactive': True}] ) fig.show()# Örnek kullanım# create_turkey_interactive_map(df)create_turkey_interactive_map(df)```